home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Palettes / TTools / TToolsPalette / TBinderList.subproj / SelChooser.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  2.9 KB  |  140 lines

  1. /* SelChooser.m
  2.  * Written By:  Thomas Burkholder
  3.  *
  4.  * You may freely copy, distribute, and reuse the code in this example.
  5.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  6.  * fitness for any particular use.
  7.  */
  8.  
  9. #import "SelChooser.h"
  10. #import "../Utilities.subproj/SortedStorage.h"
  11. #import "SelectorAgent.h"
  12. #import "SelectorBrowsingFunctions.h"
  13. #import <appkit/appkit.h>
  14.  
  15. static id selChooser;
  16.  
  17. @implementation SelChooser
  18.  
  19. + (SEL)chooseSelectorFor:anObject numArgs:(int)num startWith:(SEL)aSel
  20. {
  21.     if (!selChooser)
  22.         selChooser = [[self alloc] init];
  23.     [selChooser setNumArgs:num];
  24.     [selChooser setSelSource:anObject];
  25.     [selChooser setStartWith:aSel];
  26.     return [selChooser choose];
  27. }
  28.  
  29. - setNumArgs:(int)num
  30. {
  31.     numArgs = num;
  32.     return self;
  33. }
  34.  
  35. - setSelSource:anObject
  36. {
  37.     selSource = anObject;
  38.     return self;
  39. }
  40.  
  41. - setStartWith:(SEL)aSel
  42. {
  43.     startWith = aSel;
  44.     return self;
  45. }
  46.  
  47. - refreshFromVars
  48. {
  49.     int choice;
  50.     CAElementFilterFunc *filterFunction;
  51.  
  52.     choice = [[[popUp target] itemList] selectedRow];
  53.     [storage empty];
  54.  
  55.     switch (numArgs) {
  56.         case  -1 : filterFunction = NULL; break;
  57.         case   0 : filterFunction = (choice==2)?isAccessor:hasNoArguments;
  58.                 break;
  59.         case   1 : filterFunction = (choice==2)?isModifier:hasOneArgument;
  60.                 break;
  61.         default  : filterFunction = NULL; break;
  62.     }
  63.     [selSource methodSelectors:storage includeAncestors:(choice !=1)
  64.                 filterWith:filterFunction];
  65.     
  66.     return self;
  67. }
  68.  
  69. - popUpChanged:sender
  70. {
  71.     [self refreshFromVars];
  72.     [browser loadColumnZero];
  73.     return self;
  74. }
  75.  
  76. - (SEL)choose
  77. {
  78.     int a, res;
  79.  
  80.     [self refreshFromVars];
  81.     [window center];
  82.     [window makeKeyAndOrderFront:self];
  83.     [window makeFirstResponder:browser];
  84.     [browser loadColumnZero];
  85.     if (startWith) { char *str;
  86.         str = malloc(5+strlen(sel_getName(startWith)));
  87.         sprintf(str,"/%s",sel_getName(startWith));
  88.         [browser setPath:str];
  89.         free (str);
  90.     }
  91.     res = [NXApp runModalFor:window];
  92.     if (res == NX_RUNABORTED) return startWith;
  93.     if ((a = [[browser matrixInColumn:0] selectedRow]) >= 0) {
  94.         return (SEL)(*(SEL *)[storage elementAt:a]);
  95.     } else
  96.         return (SEL)0;
  97. }
  98.  
  99. - init
  100. {
  101.     char buf[MAXPATHLEN + 1];
  102.     id bundle;
  103.     [super init];
  104.     bundle = [NXBundle bundleForClass:[self class]];
  105.     [bundle getPath:buf forResource:"SelChooser" ofType:"nib"];
  106.     [NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
  107.     [browser setDoubleAction:@selector(ok:)];
  108.     [[[popUp target] itemList] selectCellAt:0 :0];
  109.     [[[popUp target] itemList] setTarget:self];
  110.     [[[popUp target] itemList] setAction:@selector(popUpChanged:)];
  111.     storage = [[SortedStorage alloc] initCount:0 elementSize:sizeof(SEL)
  112.                                         description:":"];
  113.     [storage setAgent:[[SelectorAgent alloc] init]];
  114.     [browser setDelegate:storage];
  115.     return self;
  116. }
  117.  
  118. - free
  119. {
  120.     // Theoretically, this would leak the nib.  In practice, the chooser
  121.     // isn't freed.
  122.     [storage free];
  123.     return [super free];
  124. }
  125.  
  126. - ok:sender
  127. {
  128.     [NXApp stopModal];
  129.     [window orderOut:self];
  130.     return self;
  131. }
  132.  
  133. - cancel:sender;
  134. {
  135.     [window orderOut:self];
  136.     [NXApp abortModal];
  137.     return self;
  138. }
  139.  
  140. @end